<template>
  <div>
    <Search :search="search" :reset="reset">
      <template v-slot:body>
        <el-form-item label="库位代号" prop="warehousecode">
          <el-input v-model="query.warehousecode" name="warehousecode" clearable />
        </el-form-item>

        <el-form-item label="库位名称" prop="repositoryname">
          <el-input v-model="query.repositoryname" name="repositoryname" clearable />
        </el-form-item>

        <el-form-item label="所属仓库编码" prop="fnumber"> 
          <el-select v-model="query.fnumber" placeholder="Select" clearable>
            <el-option v-for="item in cangkuList" :key="item.id" :label="item.fname" :value="item.fnumber" />
          </el-select>
        </el-form-item>
      </template>
    </Search>

    <div class="table-default">
      <Operate :show="open">
        <template #operate>
          <el-button type="primary" @click="print()">打印</el-button>
        </template>
      </Operate>
      <el-table :data="tableData" class="mt-3" v-loading="loading" @selection-change="handleSelectionChange" border :show-overflow-tooltip="true">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="id" label="行号" /> -->
        <el-table-column prop="warehousecode" label="库位代号" min-width="100" />
        <el-table-column prop="repositoryname" label="库位名称" min-width="100" />
        <el-table-column prop="owning_id" label="所属仓库名称" min-width="120" />
        <el-table-column prop="person" label="仓库负责人" min-width="120" />
        <el-table-column prop="remarks" label="备注" min-width="120" />
        <el-table-column prop="creator_id.username" label="创建者" min-width="150" />
        <el-table-column prop="created_at" label="创建时间" min-width="150" />
        <el-table-column prop="modifier" label="修改者" min-width="150" />
        <el-table-column prop="updated_at" label="修改时间" min-width="170" />

        <el-table-column label="操作" width="200" fixed="rigth">
          <template #default="scope">
            <Update @click="open(scope.row.id)" />
            <Destroy @click="destroy(api, scope.row.id)" />
          </template>
        </el-table-column>
      </el-table>

      <Paginate />
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>

    <el-dialog v-model="printShow" title="打印预览" width="40%">
      <barCode :barcodeList="barcodeList" :PrintNow="PrintNow" @send-data="getDataHandler"></barCode>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="warning" @click="PrintNow = true">确认打印</el-button>
          <el-button @click="printShow = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref, reactive } from 'vue'
import Create from './create.vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import barCode from './barCode.vue'
import { setTableHeight } from '/admin/utils/setHeight.js'

const api = 'warehouse/storage'

const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)
let idArray = ref([])
let barcodeList = ref([])

const printShow = ref(false)
const PrintNow = ref(false)

const handleSelectionChange = (val: any) => {
  const arr = []
  for (const obj of val) {
    arr.push(obj.id)
  }
  const uniqueSet = new Set(arr)
  idArray.value = [...uniqueSet]

  console.log('选择的数组id：', idArray.value)
}

const print = () => {
  console.log('打印', idArray.value)
  if (idArray.value.length != 0) {
    getPrint(idArray.value)
  } else {
    ElMessage.warning('请选择库位！')
  }
}

const getPrint = arr => {
  let idArr = []
  idArr = arr
  const data = {
    id: arr,
  }
  console.log('请求打印发送数据：', data)

  http.post('/warehouse/Libraryprint', data).then(r => {
    let res = r.data.data
    console.log('返回的打印', res)
    if ((res.code = '10000')) ElMessage.success('确认勾选，跳转打印')
    barcodeList.value = res.data
    console.log('返回的打印', barcodeList.value)
    printShow.value = true
  })
}

const cangkuList = ref([])
const getcangku = () => {
  //morovan.gnway.cc:1881/api/warehouse/datatype
  http: http.get('/warehouse/datatype').then(r => {
    let res = r.data.data
    console.log('获取仓库数据', res)
    cangkuList.value = res
  })
}

getcangku()

const getDataHandler = () => {
  console.log('接收到子组件传值')
  printShow.value = false
  PrintNow.value = false
}
onMounted(() => {
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>


<style>
.el-overlay-dialog {
  overflow: hidden;
}
.el-form--inline .el-form-item {
  margin-bottom: 10px;
}
.el-table {
  height: 80%;
}
</style>

